package com.example.qx.faxiana;

import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;

import android.annotation.SuppressLint;
import android.content.ContentResolver;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;

import com.example.qx.R;
import com.example.qx.database.data;
import com.example.qx.lei.cos;
import com.example.qx.lei.zhangmi;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.Objects;

public class gaitouxiang extends AppCompatActivity {
    private static final  int  a=3;
    private String uri="s";
    private zhangmi zhangmi= com.example.qx.lei.zhangmi.getInstance();
    private cos cos=new cos();
    private data data1=new data();
private ImageView tx;
private Button button;
    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gaitouxiang);
        tx=findViewById(R.id.wtouxiang);
        button=findViewById(R.id.huan);
         button.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                 selectFile();
             }
         });

    }
    public void selectFile(){
        Intent intent =new Intent(Intent.ACTION_OPEN_DOCUMENT);
        intent.setType("*/*");
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        startActivityForResult(intent.createChooser(intent,"选择文件"),a);
    }
    @Override
    protected  void onActivityResult(int request,int result,Intent data){
        super.onActivityResult(request,result,data);
        if(request==a){
            if(result==RESULT_OK){
                Uri selectedFileUri=data.getData();
                uri=selectedFileUri.toString();
                ContentResolver contentResolver=getContentResolver();
                String  c=contentResolver.getType(selectedFileUri);


                try {
                    getApplicationContext().grantUriPermission(getApplication().getPackageName(),selectedFileUri,Intent.FLAG_GRANT_READ_URI_PERMISSION|Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);

                    getContentResolver().takePersistableUriPermission(selectedFileUri,Intent.FLAG_GRANT_READ_URI_PERMISSION|Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
                }catch (SecurityException e){
                    e.printStackTrace();
                }


                if(c.startsWith("image/")) {
                    String a = String.valueOf(zhangmi.getZhang());
                    try {
                        InputStream inputStream = getContentResolver().openInputStream(selectedFileUri);
                        Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                             a = a + "tx";
                            tx.setImageBitmap(bitmap);
                            cos.delete(a, getApplication());
                            File file = getApplication().getDir("youqishuju", getApplication().MODE_PRIVATE);
                            File fiell = new File(file, a);
                            if (fiell.exists())
                                fiell.delete();
                            cos.insert(selectedFileUri, a, getApplication());
                            cos.get(a, getApplication());
                            data1.updatebjtx(uri, zhangmi.getZhang(), 1);


                    } catch (IOException | SQLException e) {
                        throw new RuntimeException(e);
                    }
                }

                final boolean tupian[]={false};
                tx.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if(!tupian[0])
                        {
                            button.setVisibility(View.GONE);
                            tx.setVisibility(View.GONE);
                            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN|View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY|View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
                            getSupportActionBar().hide();
                            ConstraintLayout.LayoutParams params2=(ConstraintLayout.LayoutParams) tx.getLayoutParams();
                            params2.height= ViewGroup.LayoutParams.MATCH_PARENT;
                            params2.width=ViewGroup.LayoutParams.MATCH_PARENT;
                            tupian[0]=true;

                        }
                        else{

                            button.setVisibility(View.VISIBLE);
                            tx.setVisibility(View.VISIBLE);
                            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
                            getSupportActionBar().show();
                            ConstraintLayout.LayoutParams params3=(ConstraintLayout.LayoutParams) tx.getLayoutParams();
                            params3.width=(int) (250*getApplicationContext().getResources().getDisplayMetrics().density);
                            params3.height=(int) (150*getApplicationContext().getResources().getDisplayMetrics().density);
                            tupian[0]=false;
                        }

                    }
                });
}}}

}